package com.example.cardgame

import android.content.Intent
import android.graphics.BitmapFactory
import android.os.Bundle
import android.util.Base64
import android.widget.Button
import android.widget.ImageView
import android.widget.TextView
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat

const val NAME = "name"
const val STUID = "stuid"
const val PHONENUMBER = "phonenumber"
const val EMAIL = "email"
const val GENDER = "gender"
const val HOBBY = "hobby"
const val BESTSCORE = "bestscore"

data class UserInfo(
    var name: String = "何梦琪",
    var gender: String = "女",
    var stuid: String = "2022110512",
    var phoneNumber: String = "13812345678",
    var email: String = "12345678901@qq.com",
    var hobby: String = "阅读",
    var bestScore: Int = 0,
    var encodedImage: String? = null
)

class MainActivity : AppCompatActivity() {
    companion object {
        // 定义请求码
        const val REQUEST_CODE_UPDATE = 1
        const val REQUEST_CODE_CAME = 2
    }

    private var userInfo = UserInfo()
    private var myHeadsuit = R.drawable.headsuit

    @Suppress("MissingInflatedId")
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        setContentView(R.layout.activity_main)
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main)) { v, insets ->
            val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
            insets
        }

        val name: TextView = findViewById(R.id.name)
        val gender: TextView = findViewById(R.id.gender)
        val stuid: TextView = findViewById(R.id.stuid)
        val phonenumber: TextView = findViewById(R.id.phonenumber)
        val email: TextView = findViewById(R.id.email)
        val hobby: TextView = findViewById(R.id.hobby)
        val headsuit: ImageView = findViewById(R.id.headsuit)
        val bestscore: TextView = findViewById(R.id.bestscore)

        // 更新个人信息
        updatePersonalInfoViews(name, gender, stuid, phonenumber, email, hobby, headsuit, bestscore)

        // 更新按钮点击事件
        findViewById<Button>(R.id.update).setOnClickListener {
            val intent = Intent(this, UpdateActivity::class.java).apply {
                putExtra(NAME, userInfo.name)
                putExtra(GENDER, userInfo.gender)
                putExtra(STUID, userInfo.stuid)
                putExtra(PHONENUMBER, userInfo.phoneNumber)
                putExtra(EMAIL, userInfo.email)
                putExtra(HOBBY, userInfo.hobby)
                putExtra("image", userInfo.encodedImage)
            }
            startActivityForResult(intent, REQUEST_CODE_UPDATE)
        }

        // 开始游戏按钮点击事件
        findViewById<Button>(R.id.startgame).setOnClickListener {
            val intent = Intent(this, GameActivity::class.java).apply {
                putExtra(BESTSCORE, userInfo.bestScore)
            }
            startActivityForResult(intent, REQUEST_CODE_CAME)
        }
    }

    private fun updatePersonalInfoViews(
        name: TextView,
        gender: TextView,
        stuNumber: TextView,
        phoneNumber: TextView,
        email: TextView,
        hobby: TextView,
        headsuit: ImageView,
        bestscore: TextView
    ) {
        name.text = String.format("%s%s", getString(R.string.name), userInfo.name)
        gender.text = String.format("%s%s", getString(R.string.gender), userInfo.gender)
        stuNumber.text = String.format("%s%s", getString(R.string.stuid), userInfo.stuid)
        phoneNumber.text = String.format("%s%s", getString(R.string.phonenumber), userInfo.phoneNumber)
        email.text = String.format("%s%s", getString(R.string.email), userInfo.email)
        hobby.text = String.format("%s%s", getString(R.string.hobby), userInfo.hobby)
        bestscore.text = String.format("%s%s", getString(R.string.bestscore), userInfo.bestScore.toString())

        if (userInfo.encodedImage != null) {
            val byteArray = Base64.decode(userInfo.encodedImage, Base64.DEFAULT)
            val imageBitmap = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.size)
            headsuit.setImageBitmap(imageBitmap)
        } else {
            headsuit.setImageResource(myHeadsuit)
        }
    }

    // 处理返回结果
    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (requestCode == REQUEST_CODE_UPDATE && resultCode == RESULT_OK) {
            data?.let {
                userInfo = userInfo.copy(
                    name = it.getStringExtra(NAME) ?: userInfo.name,
                    gender = it.getStringExtra(GENDER) ?: userInfo.gender,
                    stuid = it.getStringExtra(STUID) ?: userInfo.stuid,
                    phoneNumber = it.getStringExtra(PHONENUMBER) ?: userInfo.phoneNumber,
                    email = it.getStringExtra(EMAIL) ?: userInfo.email,
                    hobby = it.getStringExtra(HOBBY) ?: userInfo.hobby,
                    encodedImage = it.getStringExtra("image")
                )

                // 更新个人信息
                updatePersonalInfoViews(
                    findViewById(R.id.name),
                    findViewById(R.id.gender),
                    findViewById(R.id.stuid),
                    findViewById(R.id.phonenumber),
                    findViewById(R.id.email),
                    findViewById(R.id.hobby),
                    findViewById(R.id.headsuit),
                    findViewById(R.id.bestscore)
                )
            }
        } else if (requestCode == REQUEST_CODE_CAME && resultCode == RESULT_OK) {
            data?.let {
                userInfo.bestScore = it.getIntExtra("bestscore", userInfo.bestScore)
                findViewById<TextView>(R.id.bestscore).text = String.format("%s%s", getString(R.string.bestscore), userInfo.bestScore.toString())
            }
        }
    }
}
